////
////  CQPictureView.m
////  Pods
////
////  Created by 陈长青 on 2017/3/29.
////
////
//
//#import "CQPictureView.h"
//#import "ESPictureProgressView.h"
//#import <YYWebImage/YYWebImage.h>
//#import "UIAlertView+THAdditions.h"
//
//#define VB_BIG_PLAY_BTN_WH 100
//
//
//@interface CQPictureView()<UIScrollViewDelegate,CQVideoPlayerDelegate>
//
//// 当前显示图片的控件
//
//@property (nonatomic, weak) UIImageView *imageView;
//
//// 图片下载进度和错误显示
//
//@property (nonatomic, weak) ESPictureProgressView *progressView;
//
//// 视频下载进度和错误显示
//
//@property (nonatomic, weak) ESPictureProgressView *videoProgressView;
//
//// 图片宽等于屏幕宽时，等比缩放后的大小
//
//@property (nonatomic, assign) CGSize showPictureSize;
//
//// 正在执行动画
//
//@property (nonatomic, assign, getter=isShowAnim) BOOL showAnim;
//
//// 是否双击过
//
//@property (nonatomic, assign) BOOL doubleClicks;
//
//@end
//
//@implementation CQPictureView
//@synthesize fromView=_fromView;
//@synthesize urlString=_urlString;
//@synthesize reuseIdentifier=_reuseIdentifier;
//@synthesize index=_index;
//@synthesize pictureSize=_pictureSize;
//@synthesize videoPlayer=_videoPlayer;
//@synthesize playBtn=_playBtn;
//
//#pragma mark - 生命周期
//
//- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
//    
//    self = [self initWithFrame:CGRectZero];
//    if (self) {
//        
//        _reuseIdentifier = reuseIdentifier;
//    }
//    return self;
//    
//}
//
//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self setupUI];
//    }
//    return self;
//}
//
//+ (Class)layerClass {
//    
//    // 使用视频播放Layer
//    return [AVPlayerLayer class];
//}
//
//- (void)setupUI {
//    self.delegate = self;
//    self.alwaysBounceVertical = true;
//    self.backgroundColor = [UIColor clearColor];
//    self.showsHorizontalScrollIndicator = false;
//    self.showsVerticalScrollIndicator = false;
//    self.maximumZoomScale = 2;
//    
//    // 添加 imageView
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.clipsToBounds = true;
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//    imageView.frame = self.bounds;
//    imageView.userInteractionEnabled = true;
//    [self addSubview:imageView];
//    _imageView = imageView;
//    
//    // 添加进度view
//    ESPictureProgressView *progressView = [[ESPictureProgressView alloc] init];
//    [self addSubview:progressView];
//    _progressView = progressView;
//    
//    // 添加视频进度view
//    ESPictureProgressView *videoProgressView = [[ESPictureProgressView alloc] init];
//    videoProgressView.hidden = YES;
//    [self addSubview:videoProgressView];
//    _videoProgressView = videoProgressView;
//    
//    // 播放按钮
//    UIButton *bigPlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [bigPlayBtn addTarget:self action:@selector(clickedBigPlayBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:bigPlayBtn];
//    _playBtn = bigPlayBtn;
//    _playBtn.frame = CGRectMake(0, 0, VB_BIG_PLAY_BTN_WH, VB_BIG_PLAY_BTN_WH);
//    
//    // 添加监听事件
////    UITapGestureRecognizer *doubleTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleClick:)];
////    doubleTapGes.numberOfTapsRequired = 2;
////    [imageView addGestureRecognizer:doubleTapGes];
//}
//
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    self.progressView.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
//    self.videoProgressView.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
//    self.playBtn.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
//}
//
//- (void)dealloc {
//    
//    [_videoPlayer free];
//    _videoPlayer = nil;
//    NSLog(@"%s",__FUNCTION__);
//}
//
//#pragma mark - 双击放大
//
////- (void)doubleClick:(UITapGestureRecognizer *)ges {
////    CGFloat newScale = 2;
////    if (_doubleClicks) {
////        newScale = 1;
////    }
////    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[ges locationInView:ges.view]];
////    [self zoomToRect:zoomRect animated:YES];
////    _doubleClicks = !_doubleClicks;
////}
//
//- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center{
//    CGRect zoomRect;
//    zoomRect.size.height =self.frame.size.height / scale;
//    zoomRect.size.width  =self.frame.size.width  / scale;
//    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
//    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
//    return zoomRect;
//}
//
//#pragma mark - 外部方法
//
//- (void)showWithAnimationBlock:(void (^)())animationBlock completionBlock:(void (^)())completionBlock {
//    
//    CGRect fromViewRect = [_fromView convertRect:_fromView.bounds toView:nil];
////    _imageView.frame = fromViewRect;
//    self.showAnim = true;
//    [self.progressView setHidden:true];
//    self.alpha = 0;
//    [UIView animateWithDuration:0.25 animations:^{
//        
//        if (animationBlock) {
//            animationBlock();
//        }
//        self.alpha = 1;
////        self.imageView.frame = [self getImageActualFrame:self.showPictureSize];
//    } completion:^(BOOL finished) {
//        if (finished) {
//            if (completionBlock) {
//                completionBlock();
//            }
//        }
//        self.showAnim = false;
//    }];
//}
//
//- (void)hideWithAnimationBlock:(void (^)())animationBlock completionBlock:(void (^)())completionBlock {
//
//    _imageView.hidden = NO;
//    [self.progressView setHidden:true];
//    self.alpha = 1;
//    [UIView animateWithDuration:0.25 animations:^{
//
//        if (animationBlock) {
//            animationBlock();
//        }
//        self.alpha = 0;
//    } completion:^(BOOL finished) {
//        if (finished) {
//            if (completionBlock) {
//                completionBlock();
//            }
//        }
//    }];
//}
//
//- (void)cancelCurrentImageRequest {
//    
//    [_imageView yy_cancelCurrentImageRequest];
//}
//
//#pragma mark - 私有方法
//
//- (void)setVideoPlayer:(CQVideoPlayer *)videoPlayer {
//    _videoPlayer = videoPlayer;
//    _videoPlayer.delegate = self;
////    _videoPlayer.subview = self;
//    
//    // 显示图片
//    _imageView.hidden = NO;
//    
//    // 禁止缩放, 上下滑动
//    if (_videoPlayer) {
//        
//        self.delegate = nil;
//        self.alwaysBounceVertical = NO;
//    } else {
//        
//        self.delegate = self;
//        self.alwaysBounceVertical = YES;
//    }
//}
//
//// 执行动画不显示进度
//- (void)setShowAnim:(BOOL)showAnim {
//    _showAnim = showAnim;
//    if (showAnim == true) {
//        self.progressView.hidden = true;
//    }else {
//        self.progressView.hidden = self.progressView.progress == 1;
//    }
//}
//
//// 设置图片真实大小
//- (void)setPictureSize:(CGSize)pictureSize {
//    _pictureSize = pictureSize;
//    if (CGSizeEqualToSize(pictureSize, CGSizeZero)) {
//        return;
//    }
//    // 计算图片宽等于屏幕宽时，等比缩放后的大小
//    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
//    CGFloat scale = screenW / pictureSize.width;
//    CGFloat height = scale * pictureSize.height;
//    self.showPictureSize = CGSizeMake(screenW, height);
//}
//
//// 重设图片空间frame，设置self.contentSize为图片等比缩放后的大小
//- (void)setShowPictureSize:(CGSize)showPictureSize {
//    _showPictureSize = showPictureSize;
//    self.imageView.frame = [self getImageActualFrame:_showPictureSize];
//    self.contentSize = self.imageView.frame.size;
//}
//
//// 根据等比缩放后的大小，获得图片控件frame
//- (CGRect)getImageActualFrame:(CGSize)imageSize {
//    CGFloat x = 0;
//    CGFloat y = 0;
//    
//    if (imageSize.height < [UIScreen mainScreen].bounds.size.height) {
//        y = ([UIScreen mainScreen].bounds.size.height - imageSize.height) / 2;
//    }
//    return CGRectMake(x, y, imageSize.width, imageSize.height);
//}
//
//// 显示本地图片
//- (void)setLocalImage:(UIImage *)localImage {
//    _localImage = localImage;
//    [self.imageView setImage:localImage];
//    self.progressView.hidden = true;
//    self.progressView.progress = 1;
//    [self setPictureSize:localImage.size];
//}
//
//// 下载图片
//- (void)setUrlString:(NSString *)urlString {
//    _urlString = urlString;
//    [self.imageView yy_cancelCurrentImageRequest];
//    
//    // 如果没有在执行动画，那么就显示出来
//    self.progressView.progress = 0.01;
//    if (self.isShowAnim == false) {
//        // 显示出来
//        self.progressView.hidden = false;
//    }
//    
//    // 取消上一次的下载
//    self.userInteractionEnabled = false;
//    [self.imageView yy_setImageWithURL:[NSURL URLWithString:urlString] placeholder:_fromView.image options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//        CGFloat progress = (CGFloat)receivedSize / expectedSize ;
//        self.progressView.progress = progress;
//    } transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//        if (error != nil) {
//            [self.progressView showError];
//        }else {
//            if (stage == YYWebImageStageFinished) {
//                self.progressView.hidden = true;
//                self.userInteractionEnabled = true;
//                if (image != nil) {
//                    // 计算图片的大小
//                    [self setPictureSize:image.size];
//                }else {
//                    [self.progressView showError];
//                }
//                // 当下载完毕设置为1，因为如果直接走缓存的话，是不会走进度的 block 的
//                // 解决在执行动画完毕之后根据值去判断是否要隐藏
//                // 在执行显示的动画过程中：进度视图要隐藏，而如果在这个时候没有下载完成，需要在动画执行完毕之后显示出来
//                self.progressView.progress = 1;
//            }
//        }
//    }];
//}
//
//- (void)setContentSize:(CGSize)contentSize {
//    [super setContentSize:contentSize];
//    if (self.zoomScale == 1) {
//        [UIView animateWithDuration:0.25 animations:^{
//            CGPoint center = self.imageView.center;
//            center.x = self.contentSize.width * 0.5;
//            self.imageView.center = center;
//        }];
//    }
//}
//
//- (void)setPlayImage:(UIImage *)playImage {
//    
//    [_playBtn setImage:playImage forState:UIControlStateNormal];
//    _playBtn.hidden = playImage ? NO: YES;
//}
//
//- (void)clickedBigPlayBtn:(UIButton *)sender {
//    
//    sender.hidden = YES;
//    if (self.videoPlayer) {
//        [self.videoPlayer play];
//    }
//}
//
//#pragma mark - UIScrollViewDelegate
//
//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
//    return _imageView;
//}
//
//- (void)scrollViewDidZoom:(UIScrollView *)scrollView
//{
//    CGPoint center = _imageView.center;
//    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height) ? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
//    center.y = scrollView.contentSize.height * 0.5 + offsetY;
//    _imageView.center = center;
//    
//    // 如果是缩小，保证在屏幕中间
//    if (scrollView.zoomScale < scrollView.minimumZoomScale) {
//        CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width) ? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
//        center.x = scrollView.contentSize.width * 0.5 + offsetX;
//        _imageView.center = center;
//    }
//}
//
//#pragma mark - CQVideoPlayerDelegate
//
//- (void)videoPlayerPrepareToLoadAsset:(CQVideoPlayer *)videoPlayer {
//    
//    _progressView.hidden = YES;
//    _videoProgressView.hidden = NO;
//    _videoProgressView.progress = 0.01;
//    // 打开loading
//}
//
//- (void)videoPlayerProgressToLoadAsset:(CQVideoPlayer *)videoPlayer withProgress:(double)progress {
//    
//    _videoProgressView.progress = progress;
//}
//
//- (void)videoPlayerPrepareToPlay:(CQVideoPlayer *)videoPlayer {
//    
//    // 关闭loading
//    _videoProgressView.hidden = YES;
//    
//    // 关联视频播放器
//    [(AVPlayerLayer *) [self layer] setPlayer:_videoPlayer.player];
//}
//
//- (void)videoPlayerSuccessToPlay:(CQVideoPlayer *)videoPlayer {
//    
//    // 隐藏图片
//    _imageView.hidden = YES;
//}
//
//- (void)videoPlayerFailureToPlay:(CQVideoPlayer *)videoPlayer {
//    
//    _videoProgressView.hidden = YES;
//    // 关闭loading
//    
//    [UIAlertView showAlertWithTitle:@"Error"
//                            message:@"Failed to load video"];
//}
//
//@end
